package com.wjw.springsecuritydemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder pe;

    @Override
    //页面的name一定要叫username和password
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        if(!"admin".equals(s)){
            throw new UsernameNotFoundException("该用户不存在");
        }
        String password=pe.encode("aaa");
        User users=new User(s,password,
                //角色必须ROLE_前缀
                AuthorityUtils.commaSeparatedStringToAuthorityList(
                        "admin,mormal,ROLE_gm,/main.html，/main1.html,/insert,/delete"));
        //System.out.println("service登录");

        return users;
    }
}
